package com.zrrd.mcl.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.mcl.model.product.SpuImage;
import com.zrrd.mcl.model.product.SpuInfo;
import com.zrrd.mcl.model.product.SpuSaleAttr;
import com.zrrd.mcl.model.product.SpuSaleAttrValue;
import com.zrrd.mcl.product.mapper.SpuImageMapper;
import com.zrrd.mcl.product.mapper.SpuInfoMapper;
import com.zrrd.mcl.product.mapper.SpuSaleAttrMapper;
import com.zrrd.mcl.product.mapper.SpuSaleAttrValueMapper;
import com.zrrd.mcl.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements SpuInfoService {

    @Autowired
    private SpuImageMapper spuImageMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrvalueMapper;


    @Override
    @Transactional
    public void saveSpuInfo(SpuInfo spuInfo) {
        //保存spu
        this.save(spuInfo);
        //保存spu图片
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (spuImageList != null && spuImageList.size() > 0) {
            for (SpuImage spuImage : spuImageList) {
                spuImage.setSpuId(spuInfo.getId());
                spuImageMapper.insert(spuImage);
            }
        }
        //保存
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (spuSaleAttrList != null && spuSaleAttrList.size() > 0) {
            for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
                spuSaleAttr.setSpuId(spuInfo.getId());
                spuSaleAttrMapper.insert(spuSaleAttr);
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (spuSaleAttrValueList != null && spuSaleAttrValueList.size() > 0) {
                    for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrvalueMapper.insert(spuSaleAttrValue);
                    }
                }
            }
        }
    }
}
